package S2.client;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.io.IOException;

import javax.swing.ImageIcon;

import Message.Message;

public class S2ClientUI extends JFrame {

	private JPanel contentPane;
	private JTextField serveradd;
	private JTextField port;
	JTextArea notification;
	JTextArea fromserver;
	S2Client client;
	
	
	public JTextField getServeradd() {
		return serveradd;
	}

	public void setServeradd(JTextField serveradd) {
		this.serveradd = serveradd;
	}

	public JTextField getPort() {
		return port;
	}

	public void setPort(JTextField port) {
		this.port = port;
	}

	public S2Client getClient() {
		return client;
	}



	/**
	 * Create the frame.
	 * @throws IOException 
	 */
	public S2ClientUI() throws IOException {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 600, 500);
		setTitle("Client");
		contentPane = new JPanel();
		contentPane.setLayout(null);
		setContentPane(contentPane);
//		client = new S2Client(this);
		
		JLabel lblServerAddress = new JLabel("Server Address:");
		lblServerAddress.setForeground(Color.BLACK);
		lblServerAddress.setFont(new Font("Arial", Font.PLAIN, 16));
		lblServerAddress.setBounds(10, 29, 140, 26);
		contentPane.add(lblServerAddress);
		
		JLabel lblPort = new JLabel("Port:");
		lblPort.setForeground(Color.BLACK);
		lblPort.setFont(new Font("Arial", Font.PLAIN, 15));
		lblPort.setBounds(10, 80, 51, 17);
		contentPane.add(lblPort);
		
		serveradd = new JTextField();
		serveradd.setForeground(new Color(210, 105, 30));
		serveradd.setBounds(153, 34, 124, 20);
		contentPane.add(serveradd);
		serveradd.setColumns(15);
		
		port = new JTextField();
		port.setForeground(new Color(210, 105, 30));
		port.setBounds(153, 79, 124, 20);
		contentPane.add(port);
		port.setColumns(10);
		
		JButton btnConnect = new JButton("Connect");
		btnConnect.setFont(new Font("Arial", Font.PLAIN, 13));
		btnConnect.setForeground(Color.BLACK);
		btnConnect.setBounds(10, 126, 89, 23);
		btnConnect.setToolTipText("Connect to server");
		btnConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				client.setHost(serveradd.getText());
				client.setPort(Integer.valueOf(port.getText()));
				 try {
					 	client.openConnection();
					 	setNotification("Connection to server succeeded.");
					// 	client.handleMessageFromClientUI(new Message(Message.DISPLAY,"Client connected."));
				 } catch (IOException e) {setNotification("Connection to server failed."); }
			}
		});
		contentPane.add(btnConnect);
		
		JButton btnRead = new JButton("Read All Data");
		btnRead.setFont(new Font("Arial", Font.PLAIN, 13));
		btnRead.setForeground(Color.BLACK);
		btnRead.setToolTipText("");
		btnRead.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				fromserver.setText(null);
	//			Message msg1 = new Message(Message.SELECT,"select info from datatable");
				
			//			client.handleMessageFromClientUI(msg1);
						notification.setText("DB read succeeded.");
				
			}
		});
		btnRead.setBounds(10, 194, 196, 23);
		contentPane.add(btnRead);
		
		JButton btnWrite = new JButton("Write the Letter 'B' To  DB");
		btnWrite.setFont(new Font("Arial", Font.PLAIN, 13));
		btnWrite.setForeground(Color.BLACK);
		
		btnWrite.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				fromserver.setText(null);
		//		Message msg2 = new Message(Message.INSERT,"insert into datatable (info) values ('B')");
				
					   
		//			   client.handleMessageFromClientUI(msg2);
					   notification.setText("DB write succeeded.");
				
				
			}
		});
		
		
		btnWrite.setBounds(217, 194, 226, 23);
		contentPane.add(btnWrite);
		
		fromserver = new JTextArea();
		fromserver.setFont(new Font("Arial", Font.BOLD, 11));
		fromserver.setForeground(Color.BLACK);
		fromserver.setBackground(Color.WHITE);
		fromserver.setBounds(62, 245, 316, 67);
		fromserver.setEditable(false);
		contentPane.add(fromserver);
		
		notification = new JTextArea();
		notification.setForeground(new Color(210, 105, 30));
		notification.setBounds(130, 123, 222, 26);
		notification.setEditable(false);
		contentPane.add(notification);
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setBackground(new Color(240, 240, 240));
		lblNewLabel.setBounds(0, 0, 584, 472);
		setVisible(true);
		contentPane.add(lblNewLabel);
	}
	
	public void setClient(S2Client client)
	{
		this.client = client;
	}
	
	public void setNotification(String s)
	{
		notification.setText(s);
	}
	
	public void displayFromServer(String s)
	{
		fromserver.append(s + " ");
	}
	
	  public static void main(String[] args) throws IOException {
			
		S2ClientUI clientui = new S2ClientUI();
		clientui.setVisible(true);
		S2Client client = new S2Client(clientui);
		clientui.setClient(client);
	  }
}
	